<template>
  <TablePage>
    <template #top>
      <SearchForm
        title="收费岗亭管理"
        :keyword-search-data="[{ key: 'videoCameraName', label: '岗亭名称' }]"
        @search="search"
      >
        <template #extraButtons>
          <a-button type="primary" @click="showCreateModal">添加</a-button>
        </template>
      </SearchForm>
    </template>
    <SlTable :columns="columns" row-key="id" :data-source="tableData" :pagination="pagination" :loading="tableLoading">
      <template #bodyCell="{ column, record }">
        <div v-if="column.key === 'oper'">
          <a @click="showUpdateModal(record)">编辑</a>
          <a class="ml-2 text-red-400" @click="handleDelete(record)">删除</a>
        </div>
      </template>
    </SlTable>
    <CURDDialog
      id-key="id"
      :columns="columns"
      create-title="新增收费岗亭"
      update-title="编辑收费岗亭"
      delete-message="确认删除该收费岗亭吗？"
      :show-options="showOptions"
      :model="model"
      :model-form-setting="modelFormSetting"
      :model-rules="modelRules"
      :table-data="tableData"
      :oper-data="operData"
      :curd="curd"
      :reload-table="reloadTable"
    />
  </TablePage>
</template>
<script lang="ts" setup>
import { onMounted, ref, h } from "vue";
import { useI18n } from "/@/locales/useI18n";
import { useCURD } from "/@/hooks/useCURD";
import { downloadFile, handleDateRowRender } from "/@/utils/common";
import { FormItemComponentType, CURDAction } from "/@/components/CURDDialog/models";
import type { FormItemData, OperItem, ShowCURDModalOptions } from "/@/components/CURDDialog/models";
import type { TableProps } from "ant-design-vue";
import { PatternKey } from "/@/utils/verify";
import { Api, getDeviceType } from "/@/api/main/civilizedService";
import { PartialToolBoothItem, TollBoothItem } from "/@/api/main/civilizedService/models";
import { exportReq } from "/@/api/common";
import DeviceList from "./DeviceList.vue";

const { t } = useI18n();

const columns = ref<TableProps<PartialToolBoothItem>["columns"]>([
  {
    title: "岗亭编号",
    dataIndex: "tollBoothNo",
  },
  {
    title: "岗亭名称",
    dataIndex: "tollBooth",
  },
  {
    title: "备注",
    dataIndex: "remark",
  },
  {
    title: t("common.oper"),
    key: "oper",
    width: 100,
  },
]);
function search(model) {
  reloadTable({ model });
}
const model = ref<PartialToolBoothItem>({
  tollBoothNo: "",
  tollBooth: "",
  remark: "",
  tollBoothDeviceList: [],
});
const modelRules = ["tollBoothNo", "tollBooth", "tollBoothDeviceList"];
const modelFormSetting: FormItemData<PartialToolBoothItem>[] = [
  {
    key: "tollBooth",
    pattern: PatternKey.name,
    max: 100,
  },
  {
    key: "tollBoothNo",
    pattern: PatternKey.name,
    max: 100,
  },
  {
    key: "remark",
    type: FormItemComponentType.TEXTAREA,
    max: 256,
  },
  {
    key: "tollBoothDeviceList",
    label: "设备列表",
    customComponent: h(DeviceList),
  },
];

const showOptions = ref<ShowCURDModalOptions>();

const { pagination, tableData, tableLoading, curd, reloadTable } = useCURD<TollBoothItem>({
  getPageUrl: Api.tollBooths,
  updateUrl: Api.tollBooths,
  createUrl: Api.tollBooths,
  deleteUrl: Api.tollBooths,
});

const operData: OperItem<TollBoothItem>[] = [
  {
    label: t("common.edit"),
    handler: showUpdateModal,
  },
];
function showCreateModal() {
  showOptions.value = { action: CURDAction.CREATE };
}
function showUpdateModal(record) {
  showOptions.value = { action: CURDAction.UPDATE, record };
}
function handleDelete(record) {
  showOptions.value = { action: CURDAction.DELETE, record };
}
</script>
